package com.ruoyi.patrol.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.patrol.domain.PtLineSite;
import com.ruoyi.patrol.domain.PtSite;
import com.ruoyi.patrol.service.IPtLineSiteService;
import com.ruoyi.patrol.service.IPtSiteService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.patrol.domain.PtLine;
import com.ruoyi.patrol.service.IPtLineService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 巡查线路Controller
 * 
 * @author mlb
 * @date 2022-11-07
 */
@RestController
@RequestMapping("/patrol/line")
public class PtLineController extends BaseController
{
    @Autowired
    private IPtLineService ptLineService;
    @Autowired
    private IPtSiteService ptSiteService;
    @Autowired
    private IPtLineSiteService ptLineSiteService;

    /**
     * 查询巡查线路列表
     */
    @PreAuthorize("@ss.hasPermi('patrol:line:list')")
    @GetMapping("/list")
    public TableDataInfo list(PtLine ptLine)
    {
        startPage();
        List<PtLine> list = ptLineService.selectPtLineList(ptLine);
        return getDataTable(list);
    }

    /**
     * 导出巡查线路列表
     */
    @PreAuthorize("@ss.hasPermi('patrol:line:export')")
    @Log(title = "巡查线路", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(PtLine ptLine)
    {
        List<PtLine> list = ptLineService.selectPtLineList(ptLine);
        ExcelUtil<PtLine> util = new ExcelUtil<PtLine>(PtLine.class);
        return util.exportExcel(list, "line");
    }

    /**
     * 获取巡查线路详细信息
     */
    @PreAuthorize("@ss.hasPermi('patrol:line:query')")
    @GetMapping(value = { "/", "/{id}"})
    public AjaxResult getInfo(@PathVariable(value = "id", required = false) Long id)
    {
        AjaxResult ajax = AjaxResult.success();

        if (StringUtils.isNotNull(id)) {
            PtLineSite query = new PtLineSite();
            query.setLineId(id);
            List<PtLineSite> lineSites = ptLineSiteService.selectPtLineSiteList(query);
            ajax.put("lineSites", lineSites);

            ajax.put(AjaxResult.DATA_TAG, ptLineService.selectPtLineById(id));
        } else {
            PtLine line = new PtLine();
            line.setLineName("");
            line.setLineSites(new ArrayList<>());
            ajax.put(AjaxResult.DATA_TAG, line);

            PtLineSite query = new PtLineSite();
            query.setLineId(0L);
            List<PtLineSite> lineSites = ptLineSiteService.selectPtLineSiteList(query);
            ajax.put("lineSites", lineSites);//默认为1个空数据
        }
        return ajax;
    }

    /**
     * 新增巡查线路
     */
    @PreAuthorize("@ss.hasPermi('patrol:line:add')")
    @Log(title = "巡查线路", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PtLine ptLine)
    {
        // 获取当前的用户
        Long schoolId = SecurityUtils.getSchoolId();
        if (StringUtils.isNull(schoolId)) {
            return AjaxResult.error("非学校管理员不可添加学校的巡逻点位");
        }

        ptLine.setSchoolId(schoolId);
        if (ptLineService.siteNameExist(ptLine)) {
            return AjaxResult.error("本校已有重名的线路！");
        }

        return toAjax(ptLineService.insertPtLine(ptLine));
    }

    /**
     * 修改巡查线路
     */
    @PreAuthorize("@ss.hasPermi('patrol:line:edit')")
    @Log(title = "巡查线路", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PtLine ptLine)
    {
        return toAjax(ptLineService.updatePtLine(ptLine));
    }

    /**
     * 删除巡查线路
     */
    @PreAuthorize("@ss.hasPermi('patrol:line:remove')")
    @Log(title = "巡查线路", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        if (ptLineService.isLineInUse(ids[0])) {
            return AjaxResult.error("有未完成的巡查排班使用本线路，暂不能删除！");
        }
        return toAjax(ptLineService.deletePtLineByIds(ids));
    }

    @GetMapping("/optionselect")
    public AjaxResult optionselect(PtLine ptLine)
    {
        List<PtLine> posts = ptLineService.selectAll(ptLine);
        return AjaxResult.success(posts);
    }
}
